<?php

namespace app\http\middleware;

use app\http\TokenTrait;
use think\facade\Log;
use think\facade\Request;

class AuthMiddleware
{
    use TokenTrait;

    public function handle($request, \Closure $next)
    {
        $token = Request::header('Authorization');
        if (empty($token)) {
//            Log::info('test', json_encode($request->param('token', '')));
            return \json(['code' => 401, 'msg' => '无效的header:  Authorization'], 401);
        }
        $checkToken = $this->checkToken($token);
        if ($checkToken == 90001) {
            return $next($request);
        } elseif ($checkToken == 90003) {
            return \json(['code' => 401, 'msg' => 'token长时间未使用而过期，需重新登陆'], 401);
        } else {
            return \json(['code' => 401, 'msg' => 'token验证失败'], 401);
        }
    }
}
